<?php

// var_dump($_SERVER);

class Req{
    private static $headers=array();

    public static function method(){
        return strtolower($_SERVER["REQUEST_METHOD"]);
    }

    public static function user_agent(){
        return $_SERVER['HTTP_USER_AGENT'];
    }

    public static function uri(){
        return strtolower($_SERVER['REQUEST_URI']);
    }

    public static function body($key=""){
        $raw=file_get_contents('php://input');
        if (!$raw){
            return false;
        }

        $res=json_decode($raw,true);
        
        if ($key && isset($res[$key])){
            return $res[$key];
        }else{
            return false;
        }
    }

    public static function header($sstr=""){
        if (!self::$headers){
            $res=array();
            foreach ($_SERVER as $key => $value )
            {
                if (substr($key,0,4)==='HTTP'){
                    $res[strtolower(str_replace('_','-',substr($key,5)))]=$value;
                }
            }
            self::$headers=$res;
        }

        if ($sstr && isset(self::$headers[strtolower($sstr)])){
            return self::$headers[strtolower($sstr)];
        }else{
            return false;
        }
        
    }
};
